<template>
  <div>
    <div class="fr mb20">
      <el-button
        type="primary"
        :disabled="selectedRows.length === 0"
        @click="handleMany('edit', 'branch')"
      >批量审核</el-button>
    </div>
    <e-table
      ref="eTable"
      @getList="handleGetList"
      @tableCheck="handleTableCheck"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :total="total"
    >
      <el-table-column slot="selection" align="center" type="selection" width="55" />
      <el-table-column label="创建时间" slot="time" align="center">
        <template slot-scope="scope">{{ formatTime(scope.row.deptCommitTime) }}</template>
      </el-table-column>
      <el-table-column label="操作" slot="action" align="center" width="60">
        <template slot-scope="scope">
          <el-button type="text" @click="handleCheck(scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </e-table>
  </div>
</template>

<script>
import { getPartyBranchExamine } from '@/api/morality'
import { setSession } from '@/utils/util'

export default {
  data() {
    return {
      tableData: [],
      total: 1,
      loading: false,
      columns: [
        {
          slot: 'selection'
        },
        {
          label: '科室名称',
          prop: 'deptName'
        },
        {
          label: '姓名',
          prop: 'name'
        },
        {
          slot: 'time'
        },
        {
          label: '考评方案名称',
          prop: 'moralityPlanName'
        },
        {
          label: '科室评分',
          prop: 'deptScore'
        },
        {
          label: '科室评价等级',
          prop: 'deptLevelName'
        },
        {
          label: '自评分',
          prop: 'selfScore'
        },

        { slot: 'action' }
      ],
      queryParam: {
        pageNo: 1,
        pageSize: 10
      },
      selectedRows: []
    }
  },
  methods: {
    // 批量审核
    handleMany(type = 'edit', from = 'branch') {
      // 判断方案ID不同，则不能批量审核
      let planId = this.selectedRows[0].moralityPlanId
      let sure = this.selectedRows.every(v => v.moralityPlanId === planId)
      if (sure) {
        let row = this.selectedRows[0]
        setSession('moralityPeopleArr', this.selectedRows)
        this.$router.push({
          name: 'medicalEthicsCreatePerson',
          params: { type, row, from, many: true }
        })
      } else {
        this.$message.error('不同方案不能批量审核')
      }
    },
    // 审核
    handleCheck(row, type = 'edit', from = 'branch') {
      console.log('row', row)

      this.$router.push({
        name: 'medicalEthicsCreatePerson',
        params: { type, row, from }
      })
    },
    handleTableCheck(arr) {
      this.selectedRows = arr
    },
    handleEdit(row) {
      console.log('row', row)
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await getPartyBranchExamine(this.queryParam)

      if (res && res.data.records.length > 0) {
        const {
          state,
          data: { records, total }
        } = res
        this.tableData = records
        this.total = total
      } else {
        this.tableData = []
        this.total = 1
      }
      this.loading = false
    }
  },
  created() {
    this.handleGetList()
  }
}
</script>
